package com.tanhua.server.service;



import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.SearchResultVo;
import com.tanhua.domain.vo.TestSoulVo;
import com.tanhua.dubbo.api.TestSoulApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


import java.util.*;

@Service
public class TestSoulService {
    @Reference
    private TestSoulApi testSoulApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    //测试题列表
    public ResponseEntity testSoullist() {
        //1.查询列表
        List<TestSoul> testSouls =  testSouls = testSoulApi.testBysidSoullist();;

        //2.封装数据
        List<TestSoulVo> testSoulVos = new ArrayList<>();

        for (TestSoul testSoul : testSouls) {
            TestSoulVo testSoulVo = new TestSoulVo();
            BeanUtils.copyProperties(testSoul, testSoulVo);

            //根据用户id
            testSoulVo.setId(String.valueOf(testSoul.getId()));
            List<QuestionSoul> questions = testSoulApi.testQuestions(testSoul.getId());
            //从redis里面获取数据
            String reportid = redisTemplate.opsForValue().get("报告id" + UserHolder.getUserId() + testSoul.getId());
            testSoulVo.setReportId(reportid);

            List<Questions> questionsList = new ArrayList<>();
            for (QuestionSoul question : questions) {
                Questions questions1 = new Questions();
                questions1.setId(question.getId().toString());
                questions1.setQuestion(question.getQuestion());
                List<Option> options = testSoulApi.testOptionlist(question.getId());
                List<Options> optionsList = new ArrayList<>();
                for (Option option : options) {
                    Options options1 = new Options();
                    options1.setId(option.getId().toString());
                    options1.setOption(option.getTeoption());
                    optionsList.add(options1);
                }
                questions1.setOptions(optionsList);
                questionsList.add(questions1);

            }

            testSoulVo.setQuestions(questionsList);

            //添加集合
            testSoulVos.add(testSoulVo);
        }
        //返回数据
        return ResponseEntity.ok(testSoulVos);
    }

    //提交答题
    public ResponseEntity submitTestSoul(List<String> questionIds, List<String> optionIds) {
        //1.根据optionIds查询总分数
        Integer score = 0;
        for (String optionId : optionIds) {
            Option option = testSoulApi.findByoptionid(optionId);
            score += option.getScore();
        }


        //2.新建一个报告列表
        Report report = new Report();
        String uuid = String.valueOf(UUID.randomUUID());
        report.setRid(uuid);
        report.setScore(score);
        report.setUid(UserHolder.getUserId());

        //3.根据题目id查找对应的等级id
        Long soulid = testSoulApi.findByquestionid(questionIds.get(0));
        report.setSid(soulid);

        //4.先查询数据库用户是否有数据,将数据保存到数据库
        Report report1 = testSoulApi.findByuid(UserHolder.getUserId(), soulid);
        if (report1 == null) {
            //如果为空就插入
            testSoulApi.insterReport(report);
        } else {
            //如果不为空就修改
            testSoulApi.updatereport(report1.getId(), report);
        }

        //5.将报告id放到缓存里面
        redisTemplate.opsForValue().set("报告id" + UserHolder.getUserId() + soulid, uuid);
        //6.如果数据保存成功解锁下一项
        String s1 = redisTemplate.opsForValue().get("报告id" + UserHolder.getUserId() + 1);
        String s2 = redisTemplate.opsForValue().get("报告id" + UserHolder.getUserId() + 2);
        if (s1!=null) {
            //将中级解锁
            testSoulApi.updateIslock("中级");
        }
        if (s2!=null) {
           //将高级解锁
           testSoulApi.updateIslock("高级");
       }

        String message = report.getRid();
        return ResponseEntity.ok(message);
    }

    //查询结果
    public ResponseEntity SearchResult(String reportId) {
        //1.根据报告id查找自己的分数
        Integer score = testSoulApi.findByreportid(reportId);

        //2.根据自己的分数查找自己的类型
        Result result = testSoulApi.selectByScore(score);

        //3.封装数据结果
        SearchResultVo searchResultVo = new SearchResultVo();
        searchResultVo.setConclusion(result.getConclusion());

        List<SimilarYou> similarYouList = new ArrayList<>();
        List<Dimension> dimensionslist = new ArrayList<>();
        Dimension dimension = new Dimension();
        SimilarYou similarYou = new SimilarYou();
        for (int i = 0; i < 6; i++) {
            dimension.setKey("100");
            dimension.setValue("260");
            dimensionslist.add(dimension);
        }
        for (int i = 0; i < 6; i++) {
            similarYou.setId(i + 5);
            UserInfo userInfo = userInfoApi.findById(Long.valueOf(String.valueOf((new Random().nextInt(100)))));
            similarYou.setAvatar("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_" + i + ".jpg");
            similarYouList.add(similarYou);

        }

        searchResultVo.setCover(result.getCover());
        searchResultVo.setDimensions(dimensionslist);
        searchResultVo.setSimilarYou(similarYouList);

        return ResponseEntity.ok(searchResultVo);
    }
}
